package demo1;

import java.util.LinkedList;

public class MySingleLinkedList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    //头插法
    public void addHead(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
             head = node;
             return;
        }
        ListNode cur=head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next=node;
    }

    //任意位置插入
    public void addindex(int index, int data) {
        int len = size();
        if (index < 0 || index > len) {
            return;
        }
        if (index == 0) {
            addHead(data);
            return;
        }
        if (index == len) {
            addLast(data);
            return;
        }
        ListNode node =new ListNode(data);
        ListNode cur=head;
        while(index!=0){
            cur=cur.next;
            index--;
        }
       node.next=cur.next;
       cur.next=node;
    }

    //查找是否包含关键字Key是否在单链表中
    public boolean contains(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    //删除第一次出现关键字Key的结点
    public void remove(int key) {
        //先判断头节点是不是null值和key值
        if(head==null){
            return;
        }else if(head.val==key){
            head=head.next;
            return;
        }
        ListNode cur=head.next;
        ListNode prev=head;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                return;
            }
            cur=cur.next;
            prev= prev.next;
        }
    }

    //删除所有key的结点
    public void removeAllKey(int key) {
        if(head==null) {
            return;
        }
        ListNode cur=head.next;
        ListNode prev=head;
        while(cur!=null){
            if (cur.val == key) {
                prev.next=cur.next;
                cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }

    //的到单链表的长度
    public int size() {
        ListNode cur = head;
        int len = 0;
        while (cur != null) {
            cur = cur.next;
            len++;
        }
        return len;
    }

    public void clear() {
        while(head!=null){
            head=head.next;
        }
        System.out.println("==========");
//      while(head!=null){
//          ListNode cur=head.next;
//          head=null;
//          head=cur;
//      }
        System.out.println("=========");
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head=null;
        System.out.println("==========");
//        ListNode cur = head;
//        while (cur != null){
//            head = cur.next;
//            cur = null;
//            cur = head;
//        }
    }

    public void display() {
        ListNode cur=head;
        while (cur!= null) {
            System.out.println(cur.val + " ");
            cur = cur.next;
        }
    }
}
